//
//  kata_InfoViewController.m
//  CityStar
//
//  Created by wangjia on 14-7-12.
//  Copyright (c) 2014年 kata. All rights reserved.
//

#import "kata_InfoViewController.h"
#import "HTDefine.h"
#import "HttpRequestManager.h"
#import "KInterFace.h"
#import "SVProgressHUD.h"

@interface kata_InfoViewController ()

@end

@implementation kata_InfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.backgroundColor = rgba(246, 245, 241, 1);
    self.title = @"信息";
    [self.tfCode becomeFirstResponder];
    self.tfCode.delegate = self;
    // Do any additional setup after loading the view.
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 30)];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(-30, 0, 90, 30)];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftButton setTitleColor:[UIColor colorWithWhite:0.200 alpha:1.000] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor colorWithRed:0.991 green:0.532 blue:0.303 alpha:1.000] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"back_touch"] forState:UIControlStateHighlighted];
    
    [view addSubview:leftButton];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.leftBarButtonItem = barButtonItem;
}

-(void)viewWillAppear:(BOOL)animated
{
    self.tfCode.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"parkCarName"]?[[NSUserDefaults standardUserDefaults]objectForKey:@"parkCarName"]:@"";
    self.tfphone.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"parkCarPhone"]?[[NSUserDefaults standardUserDefaults]objectForKey:@"parkCarPhone"]:@"";
    [[NSUserDefaults standardUserDefaults]synchronize];
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)subMit_Touch:(id)sender
{
    NSDictionary *parameters = @{@"car":self.tfCode.text,
                                 @"phone":self.tfphone.text
                                 };
    [HttpRequestManager postMethod:KinterFace_driverAdd parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"保存成功"];
            [[NSUserDefaults standardUserDefaults]setObject:response[@"value"][0] forKey:@"parkCarID"];
            [[NSUserDefaults standardUserDefaults]setObject:self.tfCode.text forKey:@"parkCarName"];
            [[NSUserDefaults standardUserDefaults]setObject:self.tfphone.text forKey:@"parkCarPhone"];
            [[NSUserDefaults standardUserDefaults]synchronize];
            
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.tfphone becomeFirstResponder];
    return YES;
}
@end
